package com.eascs.web.p2p.investor.controller;


import java.math.BigDecimal;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.p2p.common.entity.InvestObjectEntity;
import com.eascs.p2p.common.service.IInvestObjectService;
import com.eascs.p2p.common.vo.InvestObjectVO;
import com.eascs.p2p.common.vo.ModelResult;
import com.eascs.p2p.common.wallet.utils.BigDecimalUtils;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.p2p.common.entity.vo.PageResultVO;

@RestController
@RequestMapping("/investor/rest")
public class InvestorRestController extends BaseRestController {
	private final Logger logger = LoggerFactory.getLogger(InvestorRestController.class);
	
	@Reference
	IInvestObjectService investObjectService;
	
	@RequestMapping(value = "/save", method = RequestMethod.GET)
	@ResponseBody
	public BaseRestResult<String> getOrgAllList(){
		return null;
	}
	
	 @RequestMapping(value = "/objectList", method = RequestMethod.GET)
	 @ResponseBody
	 public BaseRestResult<PageResult<InvestObjectEntity>> ajaxObjectList(String investorId, PageResultVO page) {
		 InvestObjectVO vo = new InvestObjectVO();
		 vo.setPageSize(page.getPageSize());
		 vo.setShowPage(page.getPageIndex());
		 ModelResult<PageResult<InvestObjectEntity>> modelResult = investObjectService.findFontPageInvestObjectEntity(vo);
		 if(modelResult.isSuccess()){
			 return BaseRestResult.buildRestResult(modelResult.getModel());
		 }
		 return null;
	 }
	 
	 /**预期收益*/
	@RequestMapping(value="/expectAmountHandle")
	public BaseRestResult<String> expectAmount(String investObjId,String investMoney){
		BigDecimal income = new BigDecimal(0);
		if(StringUtils.isBlank(investObjId) || StringUtils.isBlank(investMoney)){
			return BaseRestResult.buildRestResult(income.toString());
		}
		ModelResult<InvestObjectEntity> result = investObjectService.findInvestObjectDetail(investObjId);
		if (result.isSuccess()) {
			InvestObjectEntity investObject=result.getModel();
			BigDecimal principal = new BigDecimal(investMoney);// 本金
			BigDecimal rateYield = investObject.getAnnualizedYield();// 年化收益率
			BigDecimal deadline = new BigDecimal(investObject.getDeadline());// 期限
			income = BigDecimalUtils.incomeHandle(principal, rateYield, deadline, investObject.getDeadlineType());
			income = principal.add(income);
			return BaseRestResult.buildRestResult(income.toString());
		}
		return BaseRestResult.buildRestResult(income.toString());
	}
	
}
